import xadmin

from django.core.cache import cache
from apps.goods.models import Goods, GoodsSKU, GoodsType, GoodsImage, IndexGoodsBanner, IndexTypeGoodsBanner, \
    IndexPromotionBanner


# Register your models here.
class BaseModelAdmin(object):
    """公共管理模型类的抽取"""

    def save_model(self):

        obj = self.new_obj

        """新增或者更新表中的数据时调用"""
        obj.save()

        # 发出任务,让celery worker 重新生成首页静态文件
        from celery_tasks.tasks import generate_static_index_html
        generate_static_index_html.delay()

        # 清除首页的缓存数据
        context = cache.get('index_page_data')
        while context is not None:
            cache.delete('index_page_data')
            context = cache.get('index_page_data')

    def delete_model(self):
        obj = self.new_obj
        '''删除表中的数据时调用'''
        obj.delete()
        # 发出任务，让celery worker重新生成首页静态页
        from celery_tasks.tasks import generate_static_index_html
        generate_static_index_html.delay()

        # 清除首页的缓存数据
        context = cache.get('index_page_data')
        while context is None:
            cache.delete('index_page_data')
            context = cache.get('index_page_data')


class IndexPromotionBannerAdmin(BaseModelAdmin):
    """首页促销活动模型管理模型类"""
    list_display = ['name', 'url', 'image','index', 'create_time', 'update_time', 'is_delete']
    search_fields = ['name', ]
    list_filter = ['name', 'create_time']


class GoodsTypeAdmin(BaseModelAdmin):
    """商品类型模型管理模型类"""
    list_display = ['name', 'logo', 'image', 'create_time', 'update_time', 'is_delete']
    search_fields = ['name', ]
    list_filter = ['name', 'create_time']


class IndexGoodsBannerAdmin(BaseModelAdmin):
    """首页轮播商品展示模型管理模型类"""
    list_display = ['sku', 'image', 'index', 'create_time', 'update_time', 'is_delete']
    search_fields = ['sku', ]
    list_filter = ['sku', 'create_time']


class IndexTypeGoodsBannerAdmin(BaseModelAdmin):
    """首页分类商品展示模型管理模型类"""
    list_display = ['type', 'sku', 'display_type','index', 'create_time', 'update_time', 'is_delete']
    search_fields = ['type', ]
    list_filter = ['type', 'create_time']


class GoodsAdmin(object):
    list_display = ['name', 'detail', 'create_time', 'update_time', 'is_delete']
    search_fields = ['name', ]
    list_filter = ['name', 'detail', 'create_time']


class GoodsSKUAdmin(object):
    list_display = ['type', 'goods', 'name', 'desc', 'price', 'unite', 'image', 'stock', 'sales', 'status',
                    'create_time', 'update_time', 'is_delete']
    search_fields = ['type', 'goods', 'name', 'desc', 'price']
    list_filter = ['type', 'goods', 'name', 'desc', 'price', 'create_time']


class GoodsImageAdmin(object):
    list_display = ['sku', 'image', 'create_time', 'update_time', 'is_delete']
    search_fields = ['sku', ]
    list_filter = ['sku', ]


xadmin.site.register(Goods, GoodsAdmin)  # 商品SPU模型类
xadmin.site.register(GoodsSKU, GoodsSKUAdmin)  # 商品SKU模型类
xadmin.site.register(GoodsImage, GoodsImageAdmin)  # 商品图片模型类
xadmin.site.register(GoodsType, GoodsTypeAdmin)  # 商品类型模型类
xadmin.site.register(IndexTypeGoodsBanner, IndexTypeGoodsBannerAdmin)  # 首页分类商品展示模型类
xadmin.site.register(IndexPromotionBanner, IndexPromotionBannerAdmin)  # 首页促销活动模型类'
xadmin.site.register(IndexGoodsBanner, IndexGoodsBannerAdmin)  # 首页轮播商品展示模型类